import java.lang.*;
import java.util.*;
import java.io.*;
import ftp.*;

/**
 * 用于测试ftp.CoreFtp 与ftp.IOCell的简单代码
 */
class testMain{
    Timer timer;
    public static void main(String[] args) throws Exception{
	testMain fm = new testMain();
	fm.fun();
	return;
    }
    public void fun() throws Exception{
	//CoreFtp ftp = new CoreFtp("ftp.sjtu.edu.cn", 21);
	CoreFtp ftp = new CoreFtp("202.38.97.230", 21);
	ftp.debug=true;
	ftp.connect();
	BufferedReader input = new BufferedReader(
				new InputStreamReader(System.in));
	while (true){
	    System.out.print("cmd:");
	    String str = input.readLine();
	    if (str.startsWith("cd ")){
		System.out.println(ftp.cd(str.substring(3)));
	    }
	    else if (str.equals("ls")){
		String[] value = ftp.ls();
		for (int i = 0; i < value.length; i ++) 
		    System.out.println(value[i]);
	    }
	    else if (str.equals("pwd")){
		System.out.println(ftp.pwd());
	    }
	    else if (str.startsWith("download ")){
		String[] list = str.substring(9).split(" +");
		timer = new Timer();
		timer.schedule(new WatchCell(ftp.download(list[0], list[1])), 0, 2000);
	    }
	    else if (str.startsWith("filesize ")){
		System.out.println(ftp.filesize(str.substring(8)));
	    }
	    else if (str.equals("quit")){
		ftp.disconnect();
		return;
	    }
	}
    }
    class WatchCell extends TimerTask{
	IOCell cell;
	int n = 0;
	public WatchCell(IOCell newCell){
	    cell = newCell;
	    cell.debug = true; 
	}
	public void run(){
	    n ++;
	    if (cell.isFinished()){
		timer.cancel();
		System.out.println("\n" + cell.getName() + ": finished");
	    }
	    else/*
		System.out.print(cell.getName() + "(" + n  + "\t) :" 
			+ cell.getRate() + "%\t" 
			+ cell.getSpeed()/1024+ "kb/s\t"
			+ cell.getAveSpeed()/1024 + "kb/s\t"
			+ cell.getCurSpeed()/1024 + "kb/s\r");
			*/
		;
	}
    }
}
